<?php
session_start();
require_once("../config/include_tokyo.inc.php");
require_once("../class/Common_Function.php");
require_once("../class/Core_Lib_Page.php"); //翻页

//访问权限验证
$userRole = $_SESSION['user_role'];
if($userRole == 1){
	//管理员
}else{
	if($userRole > 0){
		$url = "../index.php?loginError=2";//没权限
	}else{
		$url = "../index.php?loginError=3";//没登录
	}	
	echo "<script language='javascript' type='text/javascript'>";
	echo "window.location.href='$url'";
	echo "</script>";
}

//get post value
$doFlag = $_POST["doFlag"];
$id = $_POST["id"];

//delete user
if($doFlag == "doDelete"){
	$sqlDel = "delete from customers where id= $id";
	$db->Execute($sqlDel);
	$strShow = "*****************************************<br>";
	$strShow = $strShow."删除成功：<br>";
	$strShow = $strShow."客户ID：$id<br>";
	$strShow = $strShow."*****************************************<br>";
	//echo $strShow;
}


//get customers list, 翻页
$listFlag = $_POST["listFlag"];
if($listFlag == ""){
	$listFlag = "1";
}
if($doFlag == "doSearch"){
	$listFlag = "2";
	$doFlag = "toList";
	$searchKeyword = $_POST["searchValue"];
}
$list_rows = 50; //每页显示的记录数
$sql_all = "SELECT COUNT(*) as total_rows from customers"; //default 
if($listFlag == "1"){ //全部
	$sql_all = "SELECT COUNT(*) as total_rows from customers  ";
}else if($listFlag == "2"){ //搜索
	$sql_all = "SELECT  COUNT(*) as total_rows from customers c where c.email_address like '%$searchKeyword%'";
}
$result = $db->Execute($sql_all);
while (!$result->EOF) {
	$totalRows = $result->fields['total_rows'];
	$result->MoveNext();
}
$nowPage = $_POST['nowPage'];
$nowPage = !empty($nowPage) ? $nowPage : 1;
$params = array(
	'total_rows'=>$totalRows,
	'list_rows'=>$list_rows,
	'method'    =>'ajax',
	'ajax_func_name' =>'goToPage',
	'now_page' => $nowPage 
);
$page = new Core_Lib_Page($params);
$limit_m = $page->first_row;
$limit_n = $list_rows;


$sql_list = "select * from customers order by id desc limit $limit_m,$limit_n";//default all
if($doFlag == "toList"){//翻页
	if($listFlag == "1"){ //全部
		$sql_list = "select * from customers order by id desc limit $limit_m,$limit_n";
	}else if($listFlag == "2"){ //搜索
		$sql_list = "select * from customers c where c.email_address like '%$searchKeyword%' order by id desc limit $limit_m,$limit_n";
	}
}
$result = $db->Execute($sql_list);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<!--Begin:head-->
<? $pageTitle = "客户列表"; ?>
<?php include("../head.php"); ?>
<!--End:head-->


<body>
<?php include("../topMenu.php"); ?>





<form name="listForm" action="customerList.php" method="post">
<div id="container" >	

<div class="grayTable">	
<h4>客户列表        <a href="customerInput.php">新加客户</a>  </h4>

Email:<input type="text" name="searchValue" id="searchValue" size="30" value="<?php echo $searchValue;?>"> &nbsp;
<input type="button" name="Search" value="搜 索" onclick="doSearch()">
<br><br>
<table >
<thead>
	<tr>
		<th scope="col" >ID</th>
	    <th scope="col" >Full Name</th>
		<th scope="col" >Email</th>
		<th scope="col" width=300>Address</th>
		<th scope="col" >Post Code</th>
		<th scope="col" >Tel</th>
		<th scope="col" >Site</th>
		<th scope="col" >Product</th>
		<th scope="col" >VIP Level</th>
		<th scope="col" >Detail</th>
		<th scope="col" >Action</th>

	</tr>
</thead>

<tbody>
	<?php
	$commonFunction = new Common_Function();
	while (!$result->EOF) {
		$id = $result->fields['id'];
		$firstname = $result->fields['firstname'];
		$lastname = $result->fields['lastname'];
		$email_address = $result->fields['email_address'];
		$street_address = $result->fields['street_address'];
		$city = $result->fields['city'];
		$state = $result->fields['state'];
		$country = $result->fields['country'];
		$postcode = $result->fields['postcode'];
		$tel_number = $result->fields['tel_number'];
		$vip_level = $result->fields['vip_level'];
		$site_name = $result->fields['site_name'];
		$product_name = $result->fields['product_name'];
		//---------------------------
		$strTR = "<tr >";
		$strTR = $strTR ."<td>".$id ."</td>";
		$strTR = $strTR ."<td>".$firstname." ".$lastname."</td>";
		$strTR = $strTR ."<td>".$email_address."</td>";
		$strTR = $strTR ."<td>".$street_address.",".$city.",".$state.",".$country."</td>";
		$strTR = $strTR ."<td>".$postcode."</td>";
		$strTR = $strTR ."<td>".$tel_number ."</td>";
		$strTR = $strTR ."<td>".$site_name ."</td>";
		$strTR = $strTR ."<td>".$product_name ."</td>";
		$strTR = $strTR ."<td>".$commonFunction->getVIPLevel($vip_level)."</td>";
		$strTR = $strTR ."<td align=center>"."<a href='javascript:toDetail($id)' class='button icon add'>Detail</a>";
		$strTR = $strTR ."<td align=center>"."
						  <a href='javascript:doEdit($id)' class='button icon edit'>编辑</a>
						  <a href='javascript:doDelete($id)' class='button icon remove'>删除</a>
						  ";
		$strTR =  $strTR ."</tr >";
		echo $strTR;
		$result->MoveNext();
	}
	?>
</tbody>

					
</table>

<br>

<!--Page 分页,翻页 -->
<div id="page"><?php echo $page->show(2);?></div>

</div>



</div><!--end container -->

<input type="hidden" name="doFlag" id="doFlag" >
<input type="hidden" name="id" id="id" >
<input type="hidden" name="nowPage" id="nowPage" value="<?php echo $nowPage; ?>"><!--翻页-->
</form>


<form name="inputForm" action="customerInput.php" method="post">
<input type="hidden" name="editId" id="editId" >
</form>

<form name="detailForm" action="customerDetail.php" method="post">
<input type="hidden" name="customerId" id="customerId" >
</form>



</body>




</html>

<script>

function doEdit(id){
	document.getElementById('editId').value = id;
	document.inputForm.submit();
}

function doDelete(id){
	if(confirm("是否删除这条记录,删除后不可恢复,是否继续?")){ 
		document.getElementById('doFlag').value = "doDelete";
		document.getElementById('id').value = id;
		document.listForm.submit();
	}else{
		return;
	}
}

function toDetail(id){
	document.getElementById('customerId').value = id;
	document.detailForm.submit();
}

/**翻页**/
function goToPage(nowPage){
	document.getElementById('doFlag').value = 'toList';
	document.getElementById('nowPage').value = nowPage;
	document.listForm.submit();
}

/************搜索**************************/
function doSearch(){
	document.getElementById('doFlag').value = "doSearch";	
	document.listForm.submit();
}



</script>

